package com.example.project.activity.kaishi;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.android.volley.VolleyError;
import com.example.project.R;
import com.example.project.bean.UserBean;
import com.example.project.db.DBManager;
import com.example.project.volley.ConstantUtil;
import com.example.project.volley.Params;
import com.example.project.volley.VolleyInterface;
import com.example.project.volley.VolleyRequest;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
//注册页面
public class RegisterActivity extends AppCompatActivity implements View.OnClickListener{
    private EditText edtUser,edtPass,edtTel; //用户，密码，邮箱
    private Button butOk,butCancle; //确定。取消按钮
    private ImageView ivBack;  //图标
    //由于Android边编译边生成的原理，将匹配字符串放入全局，作为静态变量可以提高效率
    public static Pattern p = Pattern.compile("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_register );
        init();
    }

    //实例化
    private void init() {
        edtUser=findViewById( R.id.edt_zcuser );
        edtPass=findViewById( R.id.edt_zcpass );
        edtTel=findViewById( R.id.edt_zctel );
        butOk=findViewById( R.id.but_ok );
        butCancle=findViewById( R.id.but_cancle );
        ivBack=findViewById( R.id.iv_back );
        butOk.setOnClickListener( this );
        butCancle.setOnClickListener( this );
        ivBack.setOnClickListener( this );
    }

    //监听事件
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.but_ok:
                volleyList();
                break;
            case R.id.but_cancle:
                finish();
                break;
            case R.id.iv_back:
                finish();
                break;
        }
    }

    //验证是否连接成功
    private void volleyList() {
        final String name=edtUser.getText().toString().trim();
        final String pass=edtPass.getText().toString().trim();
        final String tel=edtTel.getText().toString().trim();
        if(name.equals( "" ) || pass.equals( "" ) || tel.equals( "" )){
            Toast.makeText( RegisterActivity.this,"你输入的不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        if (isEmail(tel) && tel.length()<=31){

        }else {
            Toast.makeText(RegisterActivity.this,"邮箱格式错误",Toast.LENGTH_SHORT).show();
            return;
        }
        if(pass.length()<6 || pass.length()>25){
            Toast.makeText(RegisterActivity.this,"密码长度6-25位",Toast.LENGTH_SHORT).show();
            return;
        }
        VolleyRequest.RequesetPost( ConstantUtil.CHEKINGAPI, "CHEKING", Params.GetString2( name,tel ),new VolleyInterface( this ){
            @Override
            public void onMySuccess(String result) {
                System.out.println("result:"+result);
                String i1=result.substring( 0,1 ); //result=0/r/t
                System.out.println("i:"+i1);
                int i=Integer.parseInt( i1 );
                if(i==1){
                    Toast.makeText(RegisterActivity.this,"已被注册",Toast.LENGTH_SHORT).show();
                }else {
                    Register(name,pass,tel);
                }
            }
            @Override
            public void onMyError(VolleyError error) {
                Toast.makeText(RegisterActivity.this,"网络不好！！！",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onImageSuccess(Bitmap bitmap) {

            }
        } );
    }

    //注册信息
    private void Register(String name,String pass,String tel) {
        System.out.println("准备注册");
            UserBean userBean=new UserBean(  );
            userBean.setName( name );
            userBean.setPass( pass );
            userBean.setPhone( tel );
            userBean.setHead( R.drawable.h1 );
            volleyReaister(userBean);
            Intent intent=new Intent( RegisterActivity.this, LoginActivity.class );
            startActivity( intent );
    }

    //注册
    private void volleyReaister(final UserBean userBean) {
        VolleyRequest.RequesetPost( ConstantUtil.REGISTER, "refister", Params.Register(userBean),new VolleyInterface( this ){
            @Override
            public void onMySuccess(String result) {
                if(result.equals("1")){
                    Toast.makeText(RegisterActivity.this,"注册成功",Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(RegisterActivity.this,"注册失败",Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onMyError(VolleyError error) {
                Toast.makeText(RegisterActivity.this, "注册失败", Toast.LENGTH_LONG).show();
            }

            @Override
            public void onImageSuccess(Bitmap bitmap) {

            }
        } );
    }

    //验证函数优化版
    public static boolean isEmail(String email){
        if (null==email || "".equals(email)) return false;
        Matcher m = p.matcher(email);
        return m.matches();
    }

}